//
//  DKMainInfoAlertView.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/24.
//

#import "DKMainInfoAlertView.h"
#import "DKMainSigleView.h"
@interface DKMainInfoAlertView()
@property (nonatomic, weak)zhPopupController * dk_MainInfoAlertViewPop;
@property (nonatomic,strong) DKMainInfoAlertView *dk_MainInfoAlertView;
@property (nonatomic, copy)  void(^loginOutBlock)(void);
@property (nonatomic, copy)  void(^feedBackBlock)(void);
@property (nonatomic, copy)  void(^changePwdBlock)(void);
@property (nonatomic, copy)  void(^privacyAgreementBlock)(void);
    
//@property (nonatomic, copy) void(^goCheckAgreementBlock)(NSString * url);

@property (nonatomic, strong)UIImageView * dk_MainAvtarHeadImgView;
@property (nonatomic, strong)UILabel * dk_MainUserNameLab;
@end
@implementation DKMainInfoAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = DK_Hex(@"#000000");
        [self dk_addSubView];
        [self makeUserInfo];
    }
    return self;
}
- (void)dk_addSubView{
    WeakSelf(self)
    self.dk_MainAvtarHeadImgView = [[UIImageView alloc] init];
    self.dk_MainAvtarHeadImgView.userInteractionEnabled = YES;
    self.dk_MainAvtarHeadImgView.image = DK_GetImage(@"dk_MainDefaultAvatar");
    [self addSubview:self.dk_MainAvtarHeadImgView];
    [self.dk_MainAvtarHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(20)));
        make.top.equalTo(@(DK_IsWidthScale(70)));
        make.width.equalTo(@(DK_IsWidthScale(40)));
        make.height.equalTo(@(DK_IsWidthScale(40)));
    }];
    self.dk_MainUserNameLab = [[UILabel alloc] init];
    self.dk_MainUserNameLab.textColor = DK_Hex(@"#FFFFFF");
    self.dk_MainUserNameLab.font = DK_FONT_Medium(16);
    self.dk_MainUserNameLab.text = @"Hi";
    self.dk_MainUserNameLab.numberOfLines = 0;
    [self addSubview:self.dk_MainUserNameLab];
    [self.dk_MainUserNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_MainAvtarHeadImgView.mas_right).offset(DK_IsWidthScale(19));
        make.right.equalTo(@(-DK_IsWidthScale(10)));
        make.top.equalTo(self.dk_MainAvtarHeadImgView).offset(DK_IsWidthScale(13));
    }];
    DKMainSigleView * dk_MainSigleFeedBackView = [[DKMainSigleView alloc] init];
    dk_MainSigleFeedBackView.mainSigleALertType = MainSigleALertTypeFeedback;
    [self addSubview:dk_MainSigleFeedBackView];
    [dk_MainSigleFeedBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.dk_MainAvtarHeadImgView.mas_bottom).offset(DK_IsWidthScale(30));
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    dk_MainSigleFeedBackView.goBlock = ^{
        if(weakSelf.feedBackBlock){
            [weakSelf.dk_MainInfoAlertViewPop dismiss];
            weakSelf.feedBackBlock();
        }
    };
    
    DKMainSigleView * dk_MainSiglePwdView = [[DKMainSigleView alloc] init];
    dk_MainSiglePwdView.mainSigleALertType = MainSigleALertTypePwd;
    [self addSubview:dk_MainSiglePwdView];
    [dk_MainSiglePwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dk_MainSigleFeedBackView.mas_bottom);
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    dk_MainSiglePwdView.goBlock = ^{
        if(weakSelf.changePwdBlock){
            [weakSelf.dk_MainInfoAlertViewPop dismiss];
            weakSelf.changePwdBlock();
        }
    };
    
    DKMainSigleView * dk_MainSigleVersionView = [[DKMainSigleView alloc] init];
    dk_MainSigleVersionView.mainSigleALertType = MainSigleALertTypeVersion;
    [self addSubview:dk_MainSigleVersionView];
    [dk_MainSigleVersionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dk_MainSiglePwdView.mas_bottom);
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    UIButton * dk_MainAgreementBtn = [[UIButton alloc] init];
    [dk_MainAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_MainAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_MainAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_MainAgreementBtn addTarget:self action:@selector(checkAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_MainAgreementBtn];
    [dk_MainAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(@(-DK_IsWidthScale(80)));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    
    UIButton * dk_MainLoginOutAppBtn = [[UIButton alloc] init];
    [dk_MainLoginOutAppBtn setTitle:@"Logout app" forState:UIControlStateNormal];
    [dk_MainLoginOutAppBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    dk_MainLoginOutAppBtn.titleLabel.font = DK_FONT_Medium(16);
    [dk_MainLoginOutAppBtn setBackgroundColor:DK_Hex(@"#4566FF")];
    dk_MainLoginOutAppBtn.layer.cornerRadius = DK_IsWidthScale(25);
    [dk_MainLoginOutAppBtn addTarget:self action:@selector(LoginOutApp) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:dk_MainLoginOutAppBtn];
    [dk_MainLoginOutAppBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.bottom.equalTo(dk_MainAgreementBtn.mas_top).offset(-DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
}
- (void)checkAgreement{
    if(self.privacyAgreementBlock){
        [_dk_MainInfoAlertViewPop dismiss];
        self.privacyAgreementBlock();
    }
}
- (void)LoginOutApp{
    WeakSelf(self)
    if(self.loginOutBlock){
        [DKPublicHttp apiLogoutUserForAPI:@{} complete:^{
            [weakSelf.dk_MainInfoAlertViewPop dismiss];
            weakSelf.loginOutBlock();
        }];
    }
}
+ (void)dk_ShowMainInfoAlert:(UIView *)mainView Complete:(void(^)(void))loginOutBlock withFeedBack:(void(^)(void))feedBackBlock withChangePwd:(void(^)(void))changePwdBlock withPrivacyAgreement:(void(^)(void))privacyAgreementBlock{
    DKMainInfoAlertView * dk_MainInfoAlertView = [[DKMainInfoAlertView alloc] init];
    zhPopupController * mainInfoAlertCtrl = [[zhPopupController alloc] initWithView:dk_MainInfoAlertView size:CGSizeMake(DK_SCREEN_W - DK_IsWidthScale(100), DK_SCREEN_H)];
    dk_MainInfoAlertView.loginOutBlock = loginOutBlock;
    dk_MainInfoAlertView.feedBackBlock = feedBackBlock;
    dk_MainInfoAlertView.changePwdBlock = changePwdBlock;
    dk_MainInfoAlertView.privacyAgreementBlock = privacyAgreementBlock;

    mainInfoAlertCtrl.presentationStyle = zhPopupSlideStyleFromLeft;
    mainInfoAlertCtrl.layoutType = zhPopupLayoutTypeLeft;
    mainInfoAlertCtrl.presentationTransformScale = 0.56;
    mainInfoAlertCtrl.dismissonTransformScale = 0.56;
    mainInfoAlertCtrl.dismissOnMaskTouched = YES;
    dk_MainInfoAlertView.dk_MainInfoAlertViewPop = mainInfoAlertCtrl;
    [mainInfoAlertCtrl showInView:((AppDelegate*)([UIApplication sharedApplication].delegate)).window completion:^{
    }];
}
- (void)makeUserInfo{
    DKUserBaseInfoModel * userBaseInfoMdl = [DKGlobalHelper defaultManager].userBaseInfoMdl;
    if(userBaseInfoMdl.realName.length){
        self.dk_MainUserNameLab.text = [NSString stringWithFormat:@"Hi,%@!",userBaseInfoMdl.realName];
    }else{
        self.dk_MainUserNameLab.text = [NSString stringWithFormat:@"Hi,%@!",userBaseInfoMdl.phone];
    }
}
@end
